#!/usr/bin/env python
# -*- coding: utf-8 -*-
from HTMLParser import HTMLParser
import os
import codecs
class MyHTMLParser(HTMLParser):
    num = False
    lec = False
    fullList = ""
    def handle_starttag(self, tag, attrs):
        if tag == "th" and attrs == [("bgcolor","#eceaeb"),("align","right")]:
            self.num = True
        if tag == "td" and attrs == [("colspan","2")]:
            self.lec = True
    def handle_endtag(self, tag):
        if self.num and tag =="th":
            self.num = False
        if self.lec and tag == "td":
            self.lec = False
            
    def handle_data(self, data):
        if self.num:
            self.fullList =   self.fullList[:-1] +'\n' + data[:4] + data[5:] +"~"
        if self.lec:
            if (" ".join(data.split()))[::-1] == "":
                self.fullList = self.fullList + " +"
            else:
                self.fullList = self.fullList + (" ".join(data.split()))[::-1]+"+"
            
if __name__ == '__main__':
    parser = MyHTMLParser()
    fn = os.path.join(os.getcwd(), 'Data_Dumps\\TAU\\Bid\\2012')
    for file1 in os.listdir(fn):
        with codecs.open(fn + '\\' + file1, 'r',encoding = 'utf-8') as f:
            while(True):
                r = f.read()
                if r == "":
                    break
                else :
                    parser.feed(r)
        
    with codecs.open(fn + '\\2012.txt', 'w',encoding = 'utf-8') as f:
        f.write(parser.fullList)
